package mud.controller;

import mud.entity.Reply;
import mud.entity.Ticket;
import mud.support.Utility;
import mud.repository.TicketRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/ticket")
public class TicketController {
    @Autowired
    private TicketRepository repository;
    private String today = Utility.getDate();

    @GetMapping(path = "/add")
    public Reply addNewTicket(
            @RequestParam(value = "ticket") String ticket
    ) {
        Ticket t = new Ticket();
        t.setDate(today);
        t.setTime(Utility.getTime());
        t.setTicket(ticket);
        repository.save(t);
        return new Reply("Saved");
    }

    @GetMapping(path = "/find")
    public Iterable<Ticket> findByTicket(
            @RequestParam(value = "ticket") String ticket) {
        return repository.findByTicketContaining(ticket);
    }

    @GetMapping(path = "/findAll")
    public Iterable<Ticket> findAllTicket() {
        return repository.findAll();
    }
}
